using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The EducationPeriodStandardCourseTableDB class is responsible for interacting with the database to retrieve and store information
/// about EducationPeriodStandardCourseTable objects.
/// </summary>
public class EducationPeriodStandardCourseTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EducationPeriodStandardCourseTable from the underlying datasource.
/// </summary>
/// <param name="educationPeriodDetailKey">The unique EducationPeriodDetailKey of the EducationPeriodStandardCourseTable in the database.</param>
/// <returns>An EducationPeriodStandardCourseTable when the EducationPeriodDetailKey was found in the database, or null otherwise.</returns>
public static EducationPeriodStandardCourseTable GetItem(int educationPeriodDetailKey)
{
EducationPeriodStandardCourseTable myEducationPeriodStandardCourseTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", educationPeriodDetailKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEducationPeriodStandardCourseTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEducationPeriodStandardCourseTable;
}
}

/// <summary>
/// Returns a list with EducationPeriodStandardCourseTable objects.
/// </summary>
/// <returns>A generics List with the EducationPeriodStandardCourseTable objects.</returns>
public static EducationPeriodStandardCourseTableList GetList()
{
EducationPeriodStandardCourseTableList tempList = new EducationPeriodStandardCourseTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EducationPeriodStandardCourseTable objects.
/// </summary>
/// <returns>A generics List with the EducationPeriodStandardCourseTable objects.</returns>
public static EducationPeriodStandardCourseTableList GetListByCourseKey(int CourseKey)
{
EducationPeriodStandardCourseTableList tempList = new EducationPeriodStandardCourseTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableSelectListByCourseKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@courseKey", CourseKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EducationPeriodStandardCourseTable objects.
/// </summary>
/// <returns>A generics List with the EducationPeriodStandardCourseTable objects.</returns>
public static EducationPeriodStandardCourseTableList GetListByEducationPeriodKey(int EducationPeriodKey)
{
EducationPeriodStandardCourseTableList tempList = new EducationPeriodStandardCourseTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableSelectListByEducationPeriodKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@educationPeriodKey", EducationPeriodKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EducationPeriodStandardCourseTable objects.
/// </summary>
/// <returns>A generics List with the EducationPeriodStandardCourseTable objects.</returns>
public static EducationPeriodStandardCourseTableList GetListByStandardLevelKey(int StandardLevelKey)
{
EducationPeriodStandardCourseTableList tempList = new EducationPeriodStandardCourseTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableSelectListByStandardLevelKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@standardLevelKey", StandardLevelKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EducationPeriodStandardCourseTable in the database.
/// </summary>
/// <param name="myEducationPeriodStandardCourseTable">The EducationPeriodStandardCourseTable instance to save.</param>
/// <returns>The new EducationPeriodDetailKey if the EducationPeriodStandardCourseTable is new in the database or the existing EducationPeriodDetailKey when an item was updated.</returns>
public static int Save(EducationPeriodStandardCourseTable myEducationPeriodStandardCourseTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEducationPeriodStandardCourseTable.EducationPeriodDetailKey == -1)
{myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", myEducationPeriodStandardCourseTable.EducationPeriodDetailKey);
}
if (myEducationPeriodStandardCourseTable.EducationPeriodKey == null){
myCommand.Parameters.AddWithValue("@educationPeriodKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@educationPeriodKey", myEducationPeriodStandardCourseTable.EducationPeriodKey);
}
if (myEducationPeriodStandardCourseTable.StandardLevelKey == null){
myCommand.Parameters.AddWithValue("@standardLevelKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@standardLevelKey", myEducationPeriodStandardCourseTable.StandardLevelKey);
}
if (myEducationPeriodStandardCourseTable.CourseKey == null){
myCommand.Parameters.AddWithValue("@courseKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@courseKey", myEducationPeriodStandardCourseTable.CourseKey);
}
if (string.IsNullOrEmpty(myEducationPeriodStandardCourseTable.Description))
{
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myEducationPeriodStandardCourseTable.Description);
}
if (myEducationPeriodStandardCourseTable.StartDate == null){
myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@startDate", myEducationPeriodStandardCourseTable.StartDate);
}
if (myEducationPeriodStandardCourseTable.EndDate == null){
myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@endDate", myEducationPeriodStandardCourseTable.EndDate);
}
if (myEducationPeriodStandardCourseTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEducationPeriodStandardCourseTable.Status);
}
if (myEducationPeriodStandardCourseTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEducationPeriodStandardCourseTable.CreatedDateTime);
}
if (myEducationPeriodStandardCourseTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEducationPeriodStandardCourseTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EducationPeriodStandardCourseTable from the database.
/// </summary>
/// <param name="educationPeriodDetailKey">The EducationPeriodDetailKey of the EducationPeriodStandardCourseTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int educationPeriodDetailKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEducationPeriodStandardCourseTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", educationPeriodDetailKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EducationPeriodStandardCourseTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EducationPeriodStandardCourseTable FillDataRecord(IDataRecord myDataRecord )
{
EducationPeriodStandardCourseTable myEducationPeriodStandardCourseTable = new EducationPeriodStandardCourseTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodDetailKey")))
{
myEducationPeriodStandardCourseTable.EducationPeriodDetailKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodDetailKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodKey")))
{
myEducationPeriodStandardCourseTable.EducationPeriodKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
{
myEducationPeriodStandardCourseTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CourseKey")))
{
myEducationPeriodStandardCourseTable.CourseKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("CourseKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myEducationPeriodStandardCourseTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
{
myEducationPeriodStandardCourseTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
{
myEducationPeriodStandardCourseTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEducationPeriodStandardCourseTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEducationPeriodStandardCourseTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEducationPeriodStandardCourseTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEducationPeriodStandardCourseTable;
}
}

 } 
